package org.r13notes.notes;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * 
 * @author Lukasz Raczko
 *
 * Adapter class to communicate with database in which notes are stored.
 */

public class NotesDBUserAdapter {

	public NotesDBUserAdapter(Context context){
		this.context = context;
		this.DBHelper = new NotesDBHelper(context);
	}
	
	/**
	 * Method used to add note knowing only its text
	 * 
	 * @param note - text of note
	 */
	public void addNote(String note){
		 this.open();
         ContentValues initialValues = new ContentValues();
         initialValues.put(KEY_DATE, (new Date()).getTime());
         initialValues.put(KEY_NOTE, note);      
         initialValues.put(KEY_PRIORITY, 0);
         initialValues.put(KEY_USAGE, 1);
         db.insert(TABLE_NAME, null, initialValues); 
         this.close();
    }
	
	/**
	 * Method used to add note knowing its text and priority
	 * 
	 * @param note - text of note
	 * @param priority - 0-100 priority of note
	 */
	public void addNote(String note, int priority){
		 this.open();
         ContentValues initialValues = new ContentValues();
         initialValues.put(KEY_DATE, (new Date()).getTime());
         initialValues.put(KEY_NOTE, note);      
         initialValues.put(KEY_PRIORITY, priority);
         initialValues.put(KEY_USAGE, 1);
         db.insert(TABLE_NAME, null, initialValues); 
         this.close();
    }
	
	/**
	 * 
	 * @return all notes with usage == 1 from database
	 */
	public List<Note> getNotes(){
		this.openToRead();
		Cursor values = db.rawQuery("SELECT * FROM " + TABLE_NAME + " WHERE " + KEY_USAGE + "=?", new String[]{"1"});
		
		List<Note> notes = new ArrayList<Note>();
        if (values.moveToFirst() && !values.isAfterLast()) {

			while (!values.isAfterLast()) {
				int usage = values.getInt(4);
				
				if (usage == 1){
					int id = values.getInt(0);
					int date = values.getInt(1);
					String text = values.getString(2);
					int priority = values.getInt(3);
					
					Date dateFormat = new Date();
					dateFormat.setTime(date);

					Note note = new Note(text, priority, dateFormat, id);			
					
					notes.add(note);
				}

				values.moveToNext();
			}
        	
        }
        this.close();
        values.close();
        return notes;
	}

	/**
	 * 
	 * @author Lukasz Raczko
	 * 
	 * Simple DBHelper class.
	 */
	private static class NotesDBHelper extends SQLiteOpenHelper {
		NotesDBHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DICTIONARY_TABLE_CREATE);
		} 

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS users");
			onCreate(db);
		}

	}
	
	/**
	 * All open and close methods are private because I prefer to use them inside
	 * other methods in adapter, rather than in other classes. In this app, it is not
	 * needed to have database open all the time so in my opinion it can be done
	 * this way.
	 * 	
	 * @throws SQLException
	 */
	private void open() throws SQLException {
		db = DBHelper.getWritableDatabase();
	}
	
	private void openToRead() throws SQLException {
		db = DBHelper.getReadableDatabase();
	}

	private void close() {
		DBHelper.close();
	}
	
	public static final String KEY_ROWID = "id";
    public static final String KEY_DATE= "date";
    public static final String KEY_NOTE = "note";
    public static final String KEY_PRIORITY = "priority";
    public static final String KEY_USAGE = "usage"; 

	private static final int DATABASE_VERSION = 1;
	
	private static final String TABLE_NAME = "notes";
	private static final String DICTIONARY_TABLE_CREATE = "CREATE TABLE "
			+ TABLE_NAME + " (" 
			+ KEY_ROWID + " integer primary key autoincrement, "
			+ KEY_DATE + " integer not null, " 
			+ KEY_NOTE + " text not null, "
			+ KEY_PRIORITY + " integer not null, " 
			+ KEY_USAGE + " integer not null"
			+ ");";
	private static final String DATABASE_NAME = "r13notesDatabase";
	private static final String TAG = "NotesDBUserAdapter";	   
	
    private Context context = null;  
    private NotesDBHelper DBHelper;
    private SQLiteDatabase db;
}
